export class Stack<T> implements Iterable<T> {

    private data: T[];

    constructor () {
        this.data = [];
    }

    public push (item: T) {
        this.data.push(item);
    }

    public pop () {
        return this.data.pop();
    }

    public isEmpty () {
        return this.data.length === 0;
    }

    public size () {
        return this.data.length;
    }

    [Symbol.iterator](): Iterator<T, any, undefined> {
        let index = 0;
        return {
            next: () => {
                if (index < this.data.length) {
                    return { done: false, value: this.data[index++] }
                } else {
                    return { done: true, value: null }
                }
            },
            return: () => {
                return { done: true, value: null }
            }
        }
    }

}